//
//  ChatBaseMsg.m
//  SportSocial
//
//  Created by yull on 15/1/3.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "ChatBaseMsg.h"

@implementation ChatBaseMsg

- (void)setAudioStatus:(ChatMsgAudioStatus)audioStatus
{
    _audioStatus = audioStatus;
    
    if (audioStatus == ChatMsgAudioStatusNone) {
        if (self.playTimer) {
            [self.playTimer invalidate];
        }
        self.audioRestTime = self.msgLength;
    }else if (audioStatus == ChatMsgAudioStatusPlaying) {
        [self beginPlayTimer];
    }
}

- (void)beginPlayTimer
{
    if (self.playTimer) {
        [self.playTimer invalidate];
        self.playTimer = nil;
    }
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.playTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(playAudio:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] run];
    });
}

- (void)playAudio:(NSTimer *)timer
{
    NSInteger restTime = self.audioRestTime - 1;
    if (restTime > 0) {
        self.audioRestTime --;
    }else {
        self.audioStatus = ChatMsgAudioStatusNone;
    }
}

@end
